package com.house.health.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;

/**
 * @author DuanChangSuo
 * @date 2023/1/30 15:17
 * @description 类的描述和介绍
 **/
public class PasswordUtils {
    /**
     * 获取byte[]的md5值
     * @param bytes byte[]
     * @return md5
     * @throws Exception
     */
    public static byte[] md5(byte[] bytes) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(bytes);
        return md.digest();
    }

    /**
     * BASE64解密
     * @throws Exception
     */
    public static String decryptBASE64(String key) throws Exception {
        return (new BASE64Decoder()).decodeBuffer(key).toString();
    }

    /**
     * BASE64加密
     */
    public static String encryptBASE64(byte[] key) throws Exception {
        return (new BASE64Encoder()).encodeBuffer(key);
    }

    //BASE64加密和解密 ：参考的案例如下
//    public static void main(String[] args) throws Exception {
//        System.out.println(encryptBASE64("123456".getBytes()));
//        byte[] a = decryptBASE64("123456");
//        System.out.println(decryptBASE64(encryptBASE64("123456".getBytes()).toString()));
//    }
}
